home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / NGService / MMCell.m < prev    next >
Encoding:
Text File  |  1992-08-10  |  3.6 KB  |  194 lines

  1. /*
  2.  *      You may freely copy, distribute and reuse the code
  3.  *      in this example.  Scott Anguish disclaims any warranty of
  4.  *      any kind, expressed or implied, as to its fitness
  5.  *      for any particular use.
  6.  *        Please keep this notice intact
  7.  *      Written by: Scott Anguish  sanguish@digifix.com
  8.  */
  9.  
  10. #import "MMCell.h"
  11.  
  12. #import <libc.h>                      /* MAXPATHLEN */
  13. #import <stdio.h>
  14. #import <strings.h>
  15. #include <sys/types.h>
  16. #include <ctype.h>
  17. #include <stdlib.h>
  18.  
  19. #import <appkit/appkit.h>
  20.  
  21. #import <streams/streams.h>
  22.  
  23.  
  24. @implementation MMCell:Cell
  25.  
  26. - initFromFilename:(const char *)filename
  27. {
  28.     NXBeep();
  29.     [super init];
  30.     [self imageFromFilename:filename];
  31.     return self;
  32. }
  33.  
  34.  
  35. - imageFromAttachment
  36. {
  37.     return self;
  38. }
  39.  
  40.  
  41. - imageFromFilename:(const char *)filename
  42. {
  43.     return self;
  44. }
  45.  
  46.  
  47.  
  48. - free
  49. {
  50.     fprintf(stderr,"Free\n");
  51.     [theImage free];
  52.     [self removeData];
  53.     [super free];
  54.     return self;
  55. }
  56.  
  57. - calcCellSize:(NXSize *)theSize
  58. {
  59.     NXSize s;
  60.     
  61.     [theImage getSize: &s];
  62.     s.height += 4.0;
  63.     s.width += 4.0;
  64.     *theSize = s;
  65.     return self;
  66. }
  67.  
  68. - highlight:(const NXRect *)cellFrame inView:controlView lit:(BOOL)flag;
  69. {
  70.     return self;
  71. }
  72.  
  73.  
  74. - writeRichText:(NXStream *)stream forView:view
  75. {
  76.  
  77.     
  78.     NXPrintf(stream, "NXPRINTF\n");
  79.     return self;
  80. }
  81.  
  82. - readPrivate:(NXStream *)stream name:(char *)name
  83. {
  84.     
  85.  
  86.     NXScanf(stream, "%s\n", name); 
  87.     strcpy(attachmentName,name);
  88.     NXScanf(stream, "%d ", &attachmentSize);
  89.     
  90.  
  91.     attachmentData=malloc(attachmentSize);
  92.     NXRead(stream, attachmentData,attachmentSize);
  93.     return self;
  94. }
  95.  
  96. - readRichText:(NXStream *)stream forView:view
  97. {
  98.     char name[MAXPATHLEN+1];
  99.  
  100.     MMView = view;
  101.     [self readPrivate:stream name:name];
  102.     [self uudecodeAttachment];
  103.     [self imageFromAttachment];
  104.  
  105.     return self;
  106. }
  107.  
  108. - (BOOL)trackMouse:(NXEvent *)theEvent 
  109.     inRect:(const NXRect *)cellFrame 
  110.     ofView:controlView;
  111. {
  112.     char filename[MAXPATHLEN+1];
  113.     NXPoint thePoint;
  114.     NXRect theRect;
  115.  
  116.     thePoint=theEvent->location;
  117.     [MMView convertPoint:&thePoint fromView:NULL];
  118.     NXSetRect(&theRect,thePoint.x-24,thePoint.y-24,48,48);
  119.     sprintf(filename,"/tmp/%s",attachmentName);
  120.     [MMView dragFile:filename fromRect:&theRect slideBack:YES event:theEvent];
  121.  
  122.     return YES;
  123. }
  124.  
  125. - read:(NXTypedStream *)stream
  126. {
  127.     [super read:stream];
  128.     return self;
  129. };
  130.  
  131. - write:(NXTypedStream *)stream
  132. {
  133.     [super write:stream];
  134.     return self;
  135. };
  136.  
  137. - drawInside: (const NXRect *)cellFrame inView:controlView
  138. {
  139.     NXPoint    compositePlace;
  140.  
  141.  
  142.     compositePlace.x = cellFrame->origin.x+ 2.0;
  143.     compositePlace.y = NX_MAXY(cellFrame) + 2.0;
  144.     [theImage composite:NX_SOVER toPoint:&compositePlace];
  145.  
  146.     return self;
  147. }
  148.  
  149. - uudecodeAttachment
  150. {
  151.     FILE *fp;
  152.     char commandStr[BUFSIZ];
  153.     
  154.     sprintf(commandStr,"rm -f /tmp/%s.Z; /usr/bin/uudecode ",attachmentName);
  155.     fp = popen (commandStr,"w");
  156.     if (fp != (FILE *)NULL)
  157.         {
  158.         fprintf(fp,"begin 644 /tmp/%s.Z\n",attachmentName);
  159.         fwrite(attachmentData,sizeof(char),attachmentSize,fp);
  160.         fprintf(fp,"end\n");
  161.         fprintf(stderr,"Pipe Result %d \n",pclose(fp));
  162.         }
  163.         else
  164.         fprintf(stderr,"Pipe did not open \n");
  165.  
  166.     sprintf(commandStr,"rm -fR /tmp/%s; cd /tmp; zcat %s.Z | /NextApps/Mail.app/safetar xf -; rm -f %s.Z", attachmentName, attachmentName, attachmentName);
  167.     fp = popen (commandStr,"r");
  168.     if (fp != (FILE *)NULL)
  169.         {
  170.         fprintf(stderr,"Pipe Result %d \n",pclose(fp));
  171.         }
  172.         else
  173.         fprintf(stderr,"Pipe did not open \n");
  174.     return self;
  175. };
  176.  
  177. - removeData
  178. {
  179.     FILE *fp;
  180.     char commandStr[BUFSIZ];
  181.     
  182.     sprintf(commandStr,"rm -f %s", attachmentName);
  183.     fp = popen (commandStr,"r");
  184.     if (fp != (FILE *)NULL)
  185.         {
  186.         fprintf(stderr,"Pipe Result %d \n",pclose(fp));
  187.         }
  188.         else
  189.         fprintf(stderr,"Pipe did not open \n");
  190.     return self;
  191.  
  192. }
  193. @end
  194.